﻿
var dutyData = [{ id: 1, text: "A" }, { id: 2, text: "B" }];
var employeePhoto = "";
///绑定grid列表
$(document).ready(function () {
    setTimeout(initData(),1000);
});

var initData = function () {
    var dept_id = mini.get("dept_id");
    var duty_id = mini.get("duty_id");
    //依赖于edit.common.js
    //兼职部门
    fnDataGridDataBing("datagrid_dept", "/extendpage/data/jsondata.aspx?entity=v_t_base_parttimeposition&order=&condition=eid='" + instance.id + "'")
    //教育经历
    fnDataGridDataBing("datagrid_edu", "/extendpage/data/jsondata.aspx?entity=t_employee_education&order=sort&desc=No&condition=eid='" + instance.id + "'")
    //工作背景
    fnDataGridDataBing("datagrid_work", "/extendpage/data/jsondata.aspx?entity=t_employee_workback&order=sort&desc=No&condition=eid='" + instance.id + "'")
    //家庭成员
    fnDataGridDataBing("datagrid_family", "/extendpage/data/jsondata.aspx?entity=t_employee_family&order=sort&desc=No&condition=eid='" + instance.id + "'")
    //合同
    fnDataGridDataBing("datagrid_contract", "/extendpage/data/jsondata.aspx?entity=t_employee_contract&order=begin_time&condition=eid='" + instance.id + "'")
    //绑定数据到表单
    fnSetBaseInfo('t_employee_finance', 'cw_info');
    fnSetBaseInfo('t_employee_human', 'hr_info');
    fnSetBaseInfo('t_employee_backgroud', 'bg_info');
    //部门和职位
    if (getPar("id") != "") {
        var employee = fnGetSingleData("t_base_employee", "id='" + instance.id + "'");
        var data = fnGetSingleData("t_base_department", "id='" + employee[0].dept_id + "'");
        if (data && data != null) {
            dept_id.setValue(data[0].id);
            dept_id.setText(data[0].name);
        }
        //不能修改岗位，只能通过员工异动进行操作
        dept_id.disable();
        duty_id.disable();
    }

    if (instance.employee_photo == "") {
        $("#imagePhone").attr("src", "../../images/header/F_" + instance.code + ".jpg");
    }
    else {
        $("#imagePhone").attr("src", "../../"+instance.employee_photo);
    }
    onDeptChanged();//第一次加载

    ////绑定兼职岗位
    var htmlobj = fnGetSingleData("t_base_duty", "1=1");
    var _tempArrary = [];
    $(htmlobj).each(
        function (i) {
            var e = { id: htmlobj[i].id, text: htmlobj[i].name };
            _tempArrary.push(e);
        }
    );
    dutyData = _tempArrary;
}

//部门valuechanged触发
function onDeptChanged(e) {
    var dept_id = mini.get("dept_id");
    var duty_id = mini.get("duty_id");
    var id = dept_id.getValue();
    if (id != "null") {
        duty_id.load("/extendpage/data/jsondata.aspx?entity=v_t_base_duty&type=1&condition=dept_id='" + id + "'");
        //新增时默认第一个
        if (instance.duty_id == "") {
            duty_id.select(0);
        } else {
            function Duty(data) {
                var json = mini.decode(data);
                for (var i = 0; i < json.length; i++) {
                    //编辑时选中职务
                    if (json[i].id == instance.duty_id) {
                        duty_id.select(i);
                        break;
                    }

                }
            }
        }
    }
    //加载当前部门下的职务
    getAjax("/extendpage/data/jsondata.aspx?entity=v_t_base_duty&type=1&condition=dept_id='" + id + "'", null, Duty);

}

//兼职部门列表框中选择部门时触发
function onDutyDeptChanged(e) {
    //debugger
    var id = e.value;
    var htmlobj = fnGetSingleData("v_t_base_duty", "dept_id='" + id + "'");
    var _tempArrary = [];
    $(htmlobj).each(
        function (i) {
            var e = { id: htmlobj[i].id, text: htmlobj[i].name };
            _tempArrary.push(e);
        }
    );
    dutyData = _tempArrary;
    mini.get("cmbduty").load(dutyData);
}

//删除兼职部门
function fnDeptDelete(e)
{
    var rows = mini.get(e).getSelecteds();
    if (rows.length > 0) {
        if (e == "datagrid_dept") {
            if (confirm("确定要删除吗？")) {
                mini.get(e).removeRows(rows, true);
                $.each(rows, function (i) {
                    //edit.common.js的通用方法
                    fnFormRemove("t_base_parttimeposition", "id", rows[i].id);
                });
            }
        } else {
            mini.get(e).removeRows(rows, true);
        }
    } else {
        alert("请选择删除项！！！");
    }


}

//绑定信息到指定表单
//e表名 e1指定表单id

var fnSetBaseInfo = function (e, e1) {
    //依赖于/scripts/common/jquery.data.js
    var data = fnGetSingleData(e, "eid='" + instance.id + "'",1);

    //反序列化实体对象到表单
    if (data != null && typeof (data) != "undefined" && data.length > 0) {

        if (e == "t_employee_finance") {
            $("#cid").val((data[0].id));
        }
        if (e == "t_employee_human") {
            $("#rid").val((data[0].id));
        }

        if (e == "t_employee_backgroud") {
            $("#bid").val((data[0].id));
        }
        fnCustomeSetForm(e1, data[0]);
    }
}
//添加grid行 e:grid的id
function fnOpAdd(e) {
    var newRow = { "eid": instance.id };
    mini.get(e).addRow(newRow, 0);
}
//添加兼职部门
function fnDeptAdd(e) {
    var newRow = { "eid": instance.id };
    mini.get(e).addRow(newRow, 0);
}

//删除grid行 e:grid的id
function fnOpRemove(e) {
    var rows = mini.get(e).getSelecteds();
    if (rows.length > 0) {
        if (e == "datagrid_contract") {
            if (confirm("确定要删除吗？")) {
                mini.get(e).removeRows(rows, true);

            }
        } else {
            mini.get(e).removeRows(rows, true);
        }
        var delTableName="";
        switch(e)
        {
            case "datagrid_contract": delTableName="t_employee_contract";
                break;
            case "datagrid_edu": delTableName="t_employee_education";
                break;
            case "datagrid_family": delTableName="t_employee_family";
                break;
            case "datagrid_work": delTableName="t_employee_workback";
                break;
                

        }

        $.each(rows, function (i) {
            //edit.common.js的通用方法
            fnFormRemove(delTableName, "id", rows[i].id);
        });

    } else {
        alert("请选择删除项！！！");
    }
}

///grid反序列化到json -----e  grid的id
function getCommentJson(e) {
    var grid = mini.get(e);
    var data = grid.getChanges();
    return mini.encode(data);
}


//上传文件回调方法
var fnCustomUploadCallBack = function (data) {
    if (data != null) {
        employeePhoto = data[0].path.replace(/\\/g, "/") + "/" + data[0].name;
        $("#iamgePhone").attr("src", "../../"+employeePhoto);
    }
}
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

var count = 0;
function fnCustomSave() {
    if (count == 0) {
        var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
        var form = new mini.Form("form1");
        form.validate();
        if (!form.isValid()) return;
        var form_data = form.getData();
        if (form_data.name == "") {
            alert("请输入员工姓名！");
            return;
        }
        if (form_data.dept_id == "") {
            alert("请选择部门！");
            return;
        }
        form_data.employee_photo = employeePhoto;
        //系统相关
        form_data.username = fnCustomGetData("username").username;
        form_data.password = fnCustomGetData("hr_info").id_card;
        if (form_data.username == "") {
            alert("请输入用户名！");
            return;
        }
        if (form_data.password == "") {
            alert("请输入身份证号码！");
            return;
        }
        //var password = id_card.substring(id_card.length - 6, id_card.length)
        //alert(password)
        //
        var _data = fnCustomGetData("cw_info");
        _data.eid = instance.id;
        var _data2 = fnCustomGetData("hr_info");
      
        _data2.eid = instance.id;
        var _data3 = fnCustomGetData("bg_info");
        _data3.eid = instance.id;
        //debugger
        //兼职信息
        var grid = mini.get("datagrid_dept");
        var data4 = grid.getChanges();
        $.each(data4, function (i) {
            if (!data4[i].id) {
                data4[i].id = guid();
            }
        })
        //格式化数据
        fnClone(form_data, instance);

        if (typeof (instance) != "undefined" && instance != null) {
            //var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
            var json = mini.encode(instance);   //序列化成JSON  基本信息
            var json1 = mini.encode(_data); //财务信息
            json1 = json1.substring(0, json1.length - 1) + ",\"id\":\"" + $("#cid").val() + "\"}";

            var json2 = mini.encode(_data2); //人力资源信息
            json2 = json2.substring(0, json2.length - 1) + ",\"id\":\"" + $("#rid").val() + "\"}";

            var json3 = mini.encode(_data3); //报关信息
            json3 = json3.substring(0, json3.length - 1) + ",\"id\":\"" + $("#bid").val() + "\"}";
            //教育背景
            var json4 = getCommentJson('datagrid_edu');
            //工作背景
            var json5 = getCommentJson('datagrid_work');
            //家庭成员
            var json6 = getCommentJson('datagrid_family');
            //合同
            var json7 = getCommentJson('datagrid_contract');
            //兼职岗位
            var parttimepositionJson = mini.encode(data4);
            //要移除的数据
            var _rData = mini.encode(_removeData);
            json = json + "[@@@]" + json1 + "[@@@]" + json2 + "[@@@]" + json3 + "[@@@]" + json4 + "[@@@]" + json5 + "[@@@]" +

    json6 + "[@@@]" + json7 + "[@@@]" + parttimepositionJson;
            $.ajax({
                url: "/extendpage/action/datasave.aspx?entity=t_base_employee:t_employee_finance:t_employee_human:t_employee_backgroud:t_employee_education:t_employee_workback:t_employee_family:t_employee_contract:t_base_parttimeposition&flag=1&action=mutibatchsave",
                type: "post",
                data: { data: json, removeData: _rData },
                cache: false,
                success: function (text) {
                    if (text == 1) {

                        mini.hideMessageBox(msg);
                        alert("数据保存成功!")
                        fnClose();

                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    mini.hideMessageBox(msg);
                    alert(jqXHR.responseText);
                }
            });
            //mini.hideMessageBox(msg);
        }
    }

}
